<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="问题流水单号" prop="checkProblemNum"  @change="handleChange">
        <el-input
          v-model="queryParams.checkProblemNum"
          placeholder="请输入问题流水单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="approvalStatus">
        <DictRadio v-model="queryParams.approvalStatus" :radioData="approvalStatusOption" :showAll="'all'"
                   size="small" @change="handleQuery"></DictRadio>
      </el-form-item>
      <el-form-item label="复查方式" prop="reviewMethod">
        <el-select v-model="queryParams.reviewMethod" placeholder="请选择检复查方式">
          <el-option
            v-for="item in reviewMethodOption"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="复查时间" prop="reviewTime">
        <el-date-picker clearable
          v-model="queryParams.reviewTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择复查时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['check:rectificationReview:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['check:rectificationReview:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['check:rectificationReview:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['check:rectificationReview:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rectificationReviewList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="整改复查id" align="center" prop="rectificationReviewId" />
      <el-table-column label="报备单号" align="center" prop="reportNum" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="检查问题流水号" align="center" prop="checkProblemNum">
        <template slot-scope="scope">
          <router-link :to="'/check/checkProblem/detail/'+scope.row.checkProblemId" class="link-type">
            <span>{{ scope.row.checkProblemNum }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="复查方式" align="center" prop="reviewMethod":formatter="this.formatterReviewMethod" />
      <el-table-column label="审核状态" align="center" prop="approvalStatus">

        <template slot-scope="scope">
          <div>
            <span v-if="scope.row.approvalStatus == '0'" ><img src="@/assets/customer/pass.svg" title="通过"></span>
            <span v-else ><img src="@/assets/customer/reject2.svg" title="不通过"></span>
          </div>
        </template>

<!--        <template slot-scope="scope">-->
<!--          <span v-if="scope.row.approvalStatus == 0" style="color: #59DD34">通过</span>-->
<!--          <span v-if="scope.row.approvalStatus == 1" style="color: #FF0000">不通过</span>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="填写意见" align="center" prop="reviewContent" />
      <el-table-column label="所属管辖大队" align="center" prop="deptName" />
      <el-table-column label="复查人" align="center" prop="reviewByName" />
      <el-table-column label="复查时间" align="center" prop="reviewTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reviewTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['check:rectificationReview:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['check:rectificationReview:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row,scope.index)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改整改复查对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="问题流水单号" prop="checkProblemId">
          <el-select v-model="form.checkProblemId" placeholder="请选择问题流水单号" :disabled="form.rectificationReviewId != null">
            <el-option
              v-for="item in checkProblemOption"
              :key="item.checkProblemId"
              :label="item.checkProblemNum"
              :value="item.checkProblemId">
            </el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="复查方式" prop="reviewMethod">
          <el-select v-model="form.reviewMethod" placeholder="请选择检查方式">
            <el-option
              v-for="item in reviewMethodOption"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="approvalStatus">
          <template>
            <el-radio v-model="form.approvalStatus" :label="0">通过</el-radio>
            <el-radio v-model="form.approvalStatus" :label="1">不通过</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="填写意见">
          <el-input v-model="form.reviewContent" type="textarea"/>
        </el-form-item>
        <el-form-item label="复查照片及视频" prop="reviewFileId">
          <file-upload  @input="getFileList" :fileLocationName="'reviewFile'" :value="this.reviewFileData" :fileType="['jpeg','png','jpg','video','mp4']"/>
        </el-form-item>
        <el-form-item label="复查人" prop="reviewByName">
          <el-input v-model="form.reviewByName" placeholder="请输入复查人" :disabled="true"/>
        </el-form-item>
        <el-form-item label="复查时间" prop="reviewTime">
          <el-date-picker clearable
            v-model="form.reviewTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择复查时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 查看整改复查对话框 -->
    <el-dialog :title="title" :visible.sync="viewOpen" width="600px" append-to-body>
      <el-form label-width="130px">
        <el-form-item label="问题流水单号">
          {{form.checkProblemNum}}
        </el-form-item>
        <el-form-item label="复查方式" >
          {{ this.formatterReviewMethod(null,null,form.reviewMethod) }}
        </el-form-item>
        <el-form-item label="审核状态">
          {{form.approvalStatus == 0 ? '通过' : '不通过'}}
        </el-form-item>
        <el-form-item label="填写意见">
          {{form.reviewContent}}
        </el-form-item>
        <el-form-item label="复查照片及视频">
          <image-preview :value="this.reviewFileData"/>
        </el-form-item>
        <el-form-item label="复查人">
          {{form.reviewByName}}
        </el-form-item>
        <el-form-item label="复查时间">
          {{form.reviewTime}}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRectificationReview, getRectificationReview, delRectificationReview, addRectificationReview, updateRectificationReview } from "@/api/check/rectificationReview";
import { selectListCheckProblem } from "@/api/check/checkProblem";

export default {
  name: "RectificationReview",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 整改复查表格数据
      rectificationReviewList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkProblemId: null,
        reviewMethod: null,
        reviewContent: null,
        reviewFileId: null,
        roadName: null,
        direction: null,
        deptId: null,
        reviewBy: null,
        reviewTime: null,
      },
      // 表单参数
      form: {},

      //  施工检查问题  option
      checkProblemOption:[],
      //  复查方式 1(线上检查/2现场检查/3网络+现场检查)
      reviewMethodOption:[{label:'线上检查',value:1},{label:'现场检查',value:2},{label:'网络+现场检查',value:3}],
      //  审批状态
      approvalStatusOption:[{label:'通过',value:0},{label:'不通过',value:1}],
      //  整改复查 照片或视频
      reviewFileData:[],

      //  是否显示详情弹出层
      viewOpen: false,

      // 表单校验
      rules: {
        checkProblemId: [
          { required: true, message: "施工检查问题id不能为空", trigger: "blur" }
        ],
        reviewMethod: [
          { required: true, message: "复查方式不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "所属大队不能为空", trigger: "blur" }
        ],
        approvalStatus: [
          { required: true, message: "审核意见不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询整改复查列表 */
    getList() {
      this.loading = true;
      listRectificationReview(this.queryParams).then(response => {
        this.rectificationReviewList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rectificationReviewId: null,
        checkProblemId: null,
        reviewMethod: null,
        reviewContent: null,
        reviewFileId: null,
        longitude: null,
        latitude: null,
        roadName: null,
        direction: null,
        deptId: null,
        reviewBy: null,
        reviewTime: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rectificationReviewId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加整改复查";
      this.form.reviewByName = this.$store.state.user.nickName;
      //日期区间回显
      this.$set(this.form, 'reviewTime', new Date())
      this.getCheckProblemList();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rectificationReviewId = row.rectificationReviewId || this.ids
      getRectificationReview(rectificationReviewId).then(response => {
        this.form = response.data;
        this.reviewFileData = response.data.reviewFile;
        this.open = true;
        this.title = "修改整改复查";
        this.getCheckProblemList();
      });
    },

    /** 详情按钮操作 */
    handleView(row) {
      this.reset();
      const rectificationReviewId = row.rectificationReviewId || this.ids
      getRectificationReview(rectificationReviewId).then(response => {
        this.form = response.data;
        this.reviewFileData = response.data.reviewFile;
        this.viewOpen = true;
        this.title = "查看整改复查";
        this.getCheckProblemList();
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rectificationReviewId != null) {
            updateRectificationReview(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRectificationReview(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rectificationReviewIds = row.rectificationReviewId || this.ids;
      this.$modal.confirm('是否确认删除整改复查编号为"' + rectificationReviewIds + '"的数据项？').then(function() {
        return delRectificationReview(rectificationReviewIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('check/rectificationReview/export', {
        ...this.queryParams
      }, `rectificationReview_${new Date().getTime()}.xlsx`)
    },

    /** 查询施工检查问题信息列表 */
    getCheckProblemList() {
      this.loading = true;
      selectListCheckProblem({}).then(response => {
        this.checkProblemOption = response.data;
        this.loading = false;
      });
    },

    //获取附件列表
    getFileList(fileLocationName,fileIds,deleteFileId){
      this.form[fileLocationName] = fileIds;
    },

    /** 问题流水号下拉列表 change事件 */
    handleChange(val){
      //  选中的数据和options进行匹配
      var obj={}
      obj= this.checkProblemOption.find(function(i){
        return i.checkProblemId ===val
      });
      this.form.deptId = obj.deptId;
      this.form.deptName = obj.deptName;
    },
    // 格式化复查方式
    formatterReviewMethod(row, column, cellValue, index) {
      //  cellValue是当前单元格的值
      const obj = this.reviewMethodOption.find(item => item.value === cellValue)
      return obj ? obj.label : '未知'
    },
  }
};
</script>
